package cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.java.dal;

import cn.iocoder.yudao.framework.common.genCode.common.StringBuilderHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenColumnDO;
import cn.iocoder.yudao.module.xiaoBizHandler.dal.dataobject.codegen.XiaoBizHandlerCodegenTableDO;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.CommonColumn;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.handler.PathHandler;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.yudao.template.CodeGenTemplate;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.GlobalBinding;
import cn.iocoder.yudao.module.xiaoBizHandler.service.codegen.newInner.LocalBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DO implements CodeGenTemplate {

    @Autowired
    CommonColumn commonColumn;

    public String getPath(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        PathHandler pathHandler = PathHandler.builder()
                .table(table)
                .columns(columns)
                .globalBinding(globalBinding)
                .localBinding(localBinding)
                .primaryColumn(primaryColumn)
                .build();
        return pathHandler.javaModuleImplMainFilePath("dal/dataobject/" + table.getBusinessName() + "/" + table.getClassName() + "DO");
    }


    public String render(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding, XiaoBizHandlerCodegenColumnDO primaryColumn) {
        StringBuilderHandler code = new StringBuilderHandler();

        code
                .apAndBl("package " + globalBinding.getBasePackage() + ".module." + table.getModuleName() + ".dal.dataobject." + table.getBusinessName() + ";")
                .apAndBl("import lombok.*;")
                .apAndBl("import java.util.*;")
                .apAndBl("import java.math.BigDecimal;")
                .apAndBl("import com.baomidou.mybatisplus.annotation.*;")
                .apAndBl("import " + globalBinding.getBaseDOClassName() + ";")
                .bl()
                .apAndBl("/**")
                .apAndBl(" * " + table.getClassComment() + " DO")
                .apAndBl(" *")
                .apAndBl(" * @author " + table.getAuthor() + "")
                .apAndBl(" */")
                .apAndBl("@TableName(\"" + table.getTableName() + "\")")
                .apAndBl("@Data")
                .apAndBl("@EqualsAndHashCode(callSuper = true)")
                .apAndBl("@ToString(callSuper = true)")
                .apAndBl("@Builder")
                .apAndBl("@NoArgsConstructor")
                .apAndBl("@AllArgsConstructor")
                .apAndBl("public class " + table.getClassName() + "DO extends BaseDO {")
                .bl()
                .apAndBl(addColumns(table, columns, globalBinding, localBinding))
                .bl()
                .apAndBl("}")
                .bl()
        ;

        return code.toString();
    }

    private String addColumns(XiaoBizHandlerCodegenTableDO table, List<XiaoBizHandlerCodegenColumnDO> columns, GlobalBinding globalBinding, LocalBinding localBinding) {
        StringBuilderHandler code = new StringBuilderHandler();

        for (XiaoBizHandlerCodegenColumnDO column : columns) {
            if (commonColumn.isCommonColumn(column.getColumnName()))    continue;
            if (!globalBinding.getBaseDOFields().contains(column.getJavaField())) {
                code.apAndBl("    /**");
                code.apAndBl("     * " + column.getColumnComment() + "");
                if (column.getDictType() != null && !"".equals(column.getDictType())) {
                    // 处理枚举
                    code.apAndBl("     *");
                    code.apAndBl("     * 枚举 {@link TODO " + column.getDictType() + " 对应的类}");
                }
                code.apAndBl("     */");
                if (column.getPrimaryKey()) {
                    // 处理主键
                    String s = "String".equals(column.getJavaType()) ? "" : "(type = IdType.INPUT)";
                    code.apAndBl("    @TableId" + s);
                }
                code.apAndBl("    private " + column.getJavaType() + " " + column.getJavaField() + ";");
            }
        }

        return code.toString();
    }

}
